package team.seekers.hula.user.userData.service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import team.seekers.hula.common.entity.enums.UserHulaLevelEnum;
import team.seekers.hula.auth.service.MindUtils;
import team.seekers.hula.user.userData.dao.UserDataDao;
import team.seekers.hula.user.userData.entity.dao.UserDataDO;
import team.seekers.hula.user.userData.entity.vo.UserDataInfoVO;
import team.seekers.hula.user.userData.mapper.UserDataMapper;

import javax.annotation.Resource;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author hbc
 * @since 2021-02-15
 */
@Service
public class UserDataService extends ServiceImpl<UserDataMapper, UserDataDO> {

    @Resource
    private UserDataDao userDataDao;

    public UserDataInfoVO getDataInfoVO(String userId) {
        UserDataDO userDataDO = getById(userId);
        UserDataInfoVO userDataInfoVO = new UserDataInfoVO();
        BeanUtils.copyProperties(userDataDO,userDataInfoVO);

        UserHulaLevelEnum userHulaLevelEnum = UserHulaLevelEnum.getEnumByScore(userDataDO.getHulaScore());

        userDataInfoVO.setLastHulaScore(userHulaLevelEnum.getScore());
        userDataInfoVO.setNextHulaScore(userHulaLevelEnum.getNextScore());

        return userDataInfoVO;
    }

    public void addCoinAndScore(Double hulaCoin, Double score) {
        UserDataDO userDataDO = lambdaQuery()
                .select(UserDataDO::getId, UserDataDO::getHulaCoin, UserDataDO::getHulaScore)
                .eq(UserDataDO::getId, MindUtils.getUserId())
                .one();

        userDataDO.setHulaCoin(userDataDO.getHulaCoin()+hulaCoin);
        userDataDO.setHulaScore(userDataDO.getHulaScore()+score);
        updateById(userDataDO);
    }

    public void reduceMind() {
        // TODO 减少用户思考数
    }

    public void reduceWind() {
        // TODO 减少用户创作数
    }

    public void changeFollowerCount(int count, String userId) {
        UserDataDO userDataDO = lambdaQuery().select(UserDataDO::getId, UserDataDO::getFollowedCount).eq(UserDataDO::getId, userId).one();
        userDataDO.setFollowedCount(userDataDO.getFollowedCount()+count);
        updateById(userDataDO);
    }
}
